require("../../assets/fonts/iconfont.css");
require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("./reject.less");

//引入模块
const CaptchaMini = require("captcha-mini");
const dom = require("../../utils/dom");
const axios = require("axios");
console.log(dom);
document.addEventListener("DOMContentLoaded", function () {
	//创建验证码
	let captcha = new CaptchaMini();
	let codeInit = "";
	captcha.draw(dom.get("#captcha1"), (r) => {
		console.log(r);
		codeInit = r.toLowerCase();
		console.log(codeInit);
	});

	//表单验证

	dom.get("#btn").addEventListener("click", function () {
		let error = dom.get("#error");

		//手机号验证
		let tel = dom.get("#tel").value;
		let reg = /^1[3456789]\d{9}$/i;
		if (reg.test(tel)) {
			error.textContent = "";
		} else {
			error.textContent = "手机格式有误";
			return;
		}

		//验证码
		let code = dom.get("#code").value.toLowerCase();
		if (code === codeInit) {
			error.textContent = "";
		} else {
			error.textContent = "验证码不一致";
			return;
		}

		//密码
		let pasw = dom.get("#pasw").value;
		// console.log(pasw);
		if (pasw.length >= 6) {
			error.textContent = "";
		} else {
			error.textContent = "密码少于6位";
			return;
		}

		//验证密码
		let pasw2 = dom.get("#pasw2").value;
		if (pasw === pasw2) {
			error.textContent = "";
		} else {
			error.textContent = "密码不一致";
		}

		//axios通过验证，发送请求
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
				account: tel,
				password: pasw,
			})
			.then((res) => {
				// console.log(res);
				if (res.data.errno === 0) {
					location.href = "login.html";
					alert("注册成功，赶紧动起来");
				} else {
					alert("注册失败，" + res.data.message);
					console.log(res.data.message);
				}
			});
	});
});
//
